package com.action;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;

import com.base.BaseAction;
import com.entity.UserVote;
import com.entity.Vote;
import com.entity.VoteOption;
import com.entity.VoteUser;
import com.service.UserVoteService;
import com.service.VoteService;
import com.util.OptionRatio;
import com.util.Page;

@SuppressWarnings("serial")
public class UserVoteAction extends BaseAction {

	Integer vid;
	Integer oid;
	@Resource(name = "voteservice")
	VoteService vs;
	@Resource(name = "userVoteServiceImpl")
	UserVoteService uvs;

	Page p = new Page();
	List<UserVote> list;

	public String add() {
		try {
			Vote vote = vs.get(vid);
			VoteOption vo = null;
			for (VoteOption option : vote.getVoteOptions()) {
				if (option.getVoId() == oid) {
					vo = option;
				}
			}
			VoteUser voteUser = (VoteUser) getSession().getAttribute("user");
			UserVote uv = new UserVote(vote, vo, voteUser);
			uvs.save(uv);
			msg = "投票成功！";
		} catch (Exception e) {
			e.printStackTrace();
			msg = "投票失败！服务器内部出错！";
		}
		return "getmyuv";
	}

	public String getmy() {
		VoteUser voteUser = (VoteUser) getSession().getAttribute("user");
		p = uvs.getUvByUserId(voteUser.getVuid(), p);
		return "myvote";
	}

	List<OptionRatio> listor;

	public String getOptionRatio() {
		listor = uvs.getOptionRatio(vid);
		return "orjson";
	}

	public Integer getVid() {
		return vid;
	}

	public void setVid(Integer vid) {
		this.vid = vid;
	}

	public Integer getOid() {
		return oid;
	}

	public void setOid(Integer oid) {
		this.oid = oid;
	}

	public List<UserVote> getList() {
		return list;
	}

	public void setList(List<UserVote> list) {
		this.list = list;
	}

	public List<OptionRatio> getListor() {
		return listor;
	}

	public void setListor(List<OptionRatio> listor) {
		this.listor = listor;
	}

	public Page getP() {
		return p;
	}

	public void setP(Page p) {
		this.p = p;
	}
}
